package com.bookstore.service;

import com.bookstore.dao.JdbcTemplateDao;
import com.bookstore.dao.SimpleJdbcInsertDao;
import com.bookstore.entity.Author;
import com.bookstore.repository.AuthorRepository;
import org.springframework.stereotype.Service;

@Service
public class BookstoreService {

    private final JdbcTemplateDao jdbcTemplateDao;
    private final SimpleJdbcInsertDao simpleJdbcInsertDao;
    private final AuthorRepository authorRepository;

    public BookstoreService(JdbcTemplateDao jdbcTemplateDao,
            SimpleJdbcInsertDao simpleJdbcInsertDao,
            AuthorRepository authorRepository) {
        this.jdbcTemplateDao = jdbcTemplateDao;
        this.simpleJdbcInsertDao = simpleJdbcInsertDao;
        this.authorRepository = authorRepository;
    }

    public void insertAuthorGetAutoGeneratedKeyViaJdbcTemplate() {
        long pk = jdbcTemplateDao.insertAuthor(23, "Mark Janel", "Anthology");
        System.out.println("Auto generated key: " + pk);
    }

    public void insertAuthorGetAutoGeneratedKeyViaSimpleJdbcInsert() {
        long pk = simpleJdbcInsertDao.insertAuthor(42, "Olivia Goy", "Horror");
        System.out.println("Auto generated key: " + pk);
    }

    public void insertAuthorGetAutoGeneratedKeyViaGetId() {

        Author author = new Author();

        author.setAge(38);
        author.setName("Alicia Tom");
        author.setGenre("Anthology");

        authorRepository.save(author);

        long pk = author.getId();
        System.out.println("Auto generated key: " + pk);
    }
}
